package com.github.search.elasticsearch;

import com.github.search.model.PageInfo;
import com.github.search.model.Article;
import com.github.search.repository.ArticleRepository;
import com.github.search.repository.ArticleTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QueryPage {

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private ArticleTemplate articleTemplate;

    /**
     * 查询
     *
     * @param q
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo query(String q, int pageNum, int pageSize) {
        try {
            List<Article> articles = articleTemplate.queryByPage(q, pageNum, pageSize);
            Long total = articleTemplate.queryTitleCount(q);
            return new PageInfo(articles, pageNum, pageSize, total.intValue(), 8);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
